package com.sharker.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sharker.R;
import com.sharker.models.TryBeen;

import java.util.List;

/**
 * 项目名称：MyApplication2
 * 类描述：
 * 创建人：${丁江楠}
 * 创建时间：2017/4/7 12:13
 * 修改人：${丁江楠}
 * 修改时间：2017/4/7 12:13
 * 修改备注：
 */
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //获取从Activity中传递过来每个item的数据集合
    private List<TryBeen> mDatas;
    private Context context;
    private MyOnitemListener myOnitemListener;

    public MyOnitemListener getMyOnitemListener() {
        return myOnitemListener;
    }

    public void setMyOnitemListener(MyOnitemListener myOnitemListener) {
        this.myOnitemListener = myOnitemListener;
    }

    public MyAdapter(List<TryBeen> mDatas, Context context) {
        this.mDatas = mDatas;
        this.context = context;
    }


    //创建View，如果是HeaderView或者是FooterView，直接在Holder中返回
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ListHolder listHolder = new ListHolder(View.inflate(context, R.layout.item, null));
        return listHolder;
    }

    //绑定View，这里是根据返回的这个position的类型，从而进行绑定的，   HeaderView和FooterView, 就不同绑定了
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                TryBeen tryBeen = mDatas.get(position);
                ((ListHolder) holder).classname.setText(tryBeen.getTitle2());
                ((ListHolder) holder).name.setText(tryBeen.getTitle());
                ((ListHolder) holder).teacher_name.setText(tryBeen.getSpeaker());
                ((ListHolder) holder).length.setText(tryBeen.getLength()+"分");
                //判断type为几
                String type = tryBeen.getType();
                if(type.equals("1")){
                    ((ListHolder) holder).type_imageView.setBackgroundResource(R.drawable.home_listen);
                }else if(type.equals("2")){
                    ((ListHolder) holder).type_imageView.setBackgroundResource(R.drawable.home_watch);
                }
                //Glide.with(context).load(tryBeen.getImage()).bitmapTransform(new RoundedCornersTransformation(context,10,0,RoundedCornersTransformation.CornerType.ALL)).crossFade(500).into(((ListHolder) holder).imageView);
                return;
    }

    //在这里面加载ListView中的每个item的布局
    class ListHolder extends RecyclerView.ViewHolder {
        TextView classname;
        TextView name;
        ImageView imageView;
        TextView teacher_name;
        TextView length;
        ImageView type_imageView;

        public ListHolder(View itemView) {
            super(itemView);
            classname = (TextView) itemView.findViewById(R.id.classname);
            name = (TextView) itemView.findViewById(R.id.name);
            imageView = (ImageView) itemView.findViewById(R.id.item_imageView);
            teacher_name = (TextView) itemView.findViewById(R.id.teacher_name);
            length = (TextView) itemView.findViewById(R.id.listtry_length);
            type_imageView = (ImageView) itemView.findViewById(R.id.trytype_imageView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(myOnitemListener!=null){
                        myOnitemListener.OnItemclick(view,getAdapterPosition());
                    }
                }
            });
        }
    }

    //返回View中Item的个数，这个时候，总的个数应该是ListView中Item的个数加上HeaderView和FooterView
    @Override
    public int getItemCount() {
            return mDatas.size();

    }
    public interface MyOnitemListener{
        void OnItemclick(View view,int position);
    }
}
